package
	
{
	import mx.effects.easing.Exponential;
	//
	import spark.components.TextArea;
	
	import views.krsCalculatorHomeView;
	
	public class KRSMathModule
	{
		private var foo:Number = 0;
		private var total:Number = 0;
		private var memorySav:Number = 0;
		private var hypValue:Number = 1; // Hypothesis
		private var shiftValue:Number = 1;
		private var minus:Boolean = false;
		private var flagSet:Boolean = false;
		private var plusMinus:Boolean = false;
		private var doSomething:Boolean = true;
		private var clear_screen:Boolean = false;
		private var lastOperator:String = "";
		private var lastNumber:Number = 0;
		private var trigger:Boolean = false;
		private var lastEntryIsDigit:Boolean = false;
		
		public function KRSMathModule()
		{
			clearVariables();
		}
	
		public function SQRT(txtArea:TextArea):void {
			var sum:Number = 0;
			sum = Math.sqrt(Number(txtArea.text));
			txtArea.text = sum+"";
			lastEntryIsDigit = false;
		}

		public function COS(txtArea:TextArea):void {
			var sum:Number = 0;
			sum = Math.cos(Number(txtArea.text));
			txtArea.text = sum+"";
			lastEntryIsDigit = false;
		}
		
		public function SIN(txtArea:TextArea):void {
			var sum:Number = 0;
			sum = Math.sin(Number(txtArea.text));
			txtArea.text = sum+"";
			lastEntryIsDigit = false;
		}
		
		public function TAN(txtArea:TextArea):void {
			var sum:Number = 0;
			sum = Math.tan(Number(txtArea.text));
			txtArea.text = sum+"";
			lastEntryIsDigit = false;
		}
		
		public function LOG(txtArea:TextArea):void {
			var sum:Number = 0;
			sum = Math.log(Number(txtArea.text));
			txtArea.text = sum+"";
			lastEntryIsDigit = false;
		}
		
		/**
		 * borrowed from a math module online
		 */
		public function Ln(txtArea:TextArea):void {
			var valueofSearch:Number = (txtArea.text.search("Error"));
			if (valueofSearch != 0) {
				var ting:Number = (Number(txtArea.text));
				var sum:Number= 1.0
				var x_k1:Number = 0;
				var asd:Number = 0;
				if (ting <= 0) {
					txtArea.text = "Error";
				} else {
					for (var i:Number = 0; true; i++)
					{
						x_k1 = sum - ((Math.exp(sum) - ting) / Math.exp(sum));
						asd = Math.abs(x_k1 - sum);	// This value does not approach to zero, fluctuating between 10^(-5) and 10^(-6)
						if (Math.abs(x_k1 - sum) < Math.pow(10,-10)) break;	// m_PRECISION_EPSILON is 10^(-14)
						sum = x_k1;
					}
					txtArea.text = sum+""; 
				}
			}
			lastEntryIsDigit = false;
		}
		
		public function Percentage(txtArea:TextArea):void {
			var sum:Number = 0;
			sum = (Number(txtArea.text)/100);
			txtArea.text = sum+"";
			lastEntryIsDigit = false;
		}
		
		public function X2(txtArea:TextArea):void {
			var sum:Number = 0;
			sum = Number(txtArea.text) * Number(txtArea.text);
			txtArea.text = sum+"";
			lastEntryIsDigit = false;
		}
		
		public function additionOperation(txtArea:TextArea):Boolean {
			var num:Number = Number(txtArea.text);

			if (txtArea.text.length > 0 && !flagSet && lastEntryIsDigit) {
				total = total + num;
				lastOperator = "+";
				txtArea.text = total+"";
				trigger = false;
				flagSet = true;
				lastEntryIsDigit = false;
			}

			return true;
		}
		
		public function subtractionOperation(txtArea:TextArea):Boolean {
			var num:Number = Number(txtArea.text);

			if (txtArea.text.length > 0 && !flagSet && lastEntryIsDigit) {
				total = total + num;
				lastOperator = "-";
				txtArea.text = total+"";
				trigger = false;
				flagSet = true;
				lastEntryIsDigit = false;
			}

			return true;
		}
		
		public function equals_operation(txtArea:TextArea):void {
			var sum:Number = 0;
		 if (txtArea.text.length > 0 ) {
			 if (!trigger) {
				 lastNumber = Number(txtArea.text);
				 trigger = true;
			 }
				if (lastOperator.toString() == "+") {
					sum = lastNumber + total;
				} else 
					if (lastOperator.toString() == "-") {
						sum = total - lastNumber;
					} else 
						if (lastOperator.toString() == "*") {
							sum = total * lastNumber;
						} else
							if (lastOperator.toString() == "/") {
								sum = total / lastNumber;
							}
				txtArea.text = sum+""; // update the display
			}
		 	flagSet = false;
			total = sum;
			lastEntryIsDigit = false;
			
		}
		
		public function setLastNumberEntered(value:Boolean) :void {
			lastEntryIsDigit = value;
		}
		
		public function clearVariables() :void {
			flagSet = false;
			minus = false;
			plusMinus = false;
			doSomething = false;
			foo = 0;
			total = 0;
			hypValue = 1;
			total = 0;
			lastNumber = 0;
			lastEntryIsDigit = false;
		}
		
	}
}